package com.pingba.serviceImpl.msg;

import com.pingba.common.Constant;
import com.pingba.dao.BaseDao;
import com.pingba.dao.msg.MsgApplyDao;
import com.pingba.entity.User;
import com.pingba.entity.msg.MsgApply;
import com.pingba.entity.msg.MsgSend;
import com.pingba.service.msg.MsgApplyService;
import com.pingba.serviceImpl.BaseServiceImpl;
import com.pingba.view.apply.MsgSendWithApplyInfoView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2015/11/30 0030.
 */
@Service("MsgApplyService")
public class MsgApplyServiceImpl extends BaseServiceImpl<MsgApply, Long> implements MsgApplyService {

    @Autowired
    private MsgApplyDao msgApplyDao;

    private static Logger logger = Logger.getLogger(MsgApplyServiceImpl.class);
    @Override
    public BaseDao<MsgApply, Long> getDao() {
        return msgApplyDao;
    }

    @Override
    public void agree(MsgApply apply) {
        if (apply.getStatus() != MsgApply.ApplyAgree) {
            logger.error(Constant.Log.MsgApplyAgreeError +apply.getSid() +  " , the value is: " + apply.getStatus());
            return;
        }
        msgApplyDao.updateStatus(apply);
    }

    @Override
    public void refuse(MsgApply apply) {
        if (apply.getStatus() != MsgApply.ApplyRefuse) {
            logger.error(Constant.Log.MsgApplyRefuseError + apply.getSid() + " , but the value is: " + apply.getStatus());
            return;
        }
        msgApplyDao.updateStatus(apply);
    }

    @Override
    public void setReaded(User msgOwner) {
        msgApplyDao.setReaded(msgOwner);
    }

    @Override
    public List<MsgApply> getUnReadList(User msgOwner) {
        return msgApplyDao.getUnReadList(msgOwner);
    }

    @Override
    public Map<Long,Map<String, Object>> myApplies(User applicant) {
        List<MsgSendWithApplyInfoView> viewList = msgApplyDao.myApplyWithMsgSend(applicant);
        Map<Long,Map<String, Object>> result = new HashMap<Long, Map<String, Object>>();
        String msgSendkey = "msgSend";
        String msgApplyKey = "msgApply";
        for (MsgSendWithApplyInfoView view : viewList) {
            System.out.println(view);
            Map<String, Object> tmp =  result.get(view.getAid());
            if (tmp == null) {
                tmp = new HashMap<String, Object>();
                MsgSend send = new MsgSend();
                send.setSid(view.getSid());
                send.setUserId(view.getUserId());
                send.setUserName(view.getUserName());
                send.setHeadUrl(view.getHeadUrl());
                send.setLongitude(view.getLongitude());
                send.setLatitude(view.getLatitude());
                send.setLocation(view.getLocation());
                send.setContent(view.getContent());
                send.setTitle(view.getTitle());
                send.setImgUrl1(view.getImgUrl1());
                send.setImgUrl2(view.getImgUrl2());
                send.setPublishTime(view.getPublishTime());
                tmp.put(msgSendkey,send);
                tmp.put(msgApplyKey, new ArrayList<MsgApply>());
                result.put(view.getSid(),tmp);
            }
            MsgApply apply = new MsgApply();
            apply.setSid(view.getAid());
            apply.setApplicantId(view.getApplicantId());
            apply.setApplicantName(view.getApplicantName());
            apply.setApplicantHeadUrl(view.getApplicantHeadUrl());
            apply.setApplicantContent(view.getApplicantContent());
            apply.setStatus(view.getStatus());
            apply.setApplyTime(view.getApplyTime());
            List<MsgApply> applyList = (List<MsgApply>) tmp.get(msgApplyKey);
            applyList.add(apply);
        }

        return result;
    }

    @Override
    public boolean deleteMyMsgApply(long msgApplyId, long userId) {
        msgApplyDao.deleteMyMsgApply(msgApplyId, userId);
        return true;
    }
}
